//
//  PushBarrageViewController.m
//  商城
//
//  Created by runone02 on 2018/9/3.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "PushBarrageViewController.h"
#import "BulletView.h"
#import "BulletManager.h"


@interface PushBarrageViewController ()
@property(nonatomic,strong)BulletManager *manager;
@end

@implementation PushBarrageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadTopview];
 
}

- (void)loadTopview{
    UIView *topView = [[UIView alloc]init];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kTopBarHeight+10);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(20);
    }];
    
    UIButton *addaBarrage = [UIButton buttonWithType:UIButtonTypeCustom];
    [addaBarrage setTitle:@"添加一条弹幕" forState:UIControlStateNormal];
    [addaBarrage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [addaBarrage addTarget:self action:@selector(addaBarrage:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:addaBarrage];
    [addaBarrage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(topView);
        make.height.mas_equalTo(topView);
        make.width.mas_equalTo(SCREEN_WIDTH/3);
    }];
    
    UIButton *Stop = [UIButton buttonWithType:UIButtonTypeCustom];
    [Stop setTitle:@"停止" forState:UIControlStateNormal];
    [Stop setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [Stop addTarget:self action:@selector(Stop:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:Stop];
    [Stop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(topView);
        make.left.mas_equalTo(topView);
        make.height.mas_equalTo(topView);
        make.width.mas_equalTo(SCREEN_WIDTH/3);
    }];
    
    UIButton *start = [UIButton buttonWithType:UIButtonTypeCustom];
    [start setTitle:@"重置" forState:UIControlStateNormal];
    [start setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [Stop addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:start];
    [start mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(topView);
        make.right.mas_equalTo(topView);
        make.height.mas_equalTo(topView);
        make.width.mas_equalTo(SCREEN_WIDTH/3);
    }];
    
    self.manager = [[BulletManager alloc]init];
    self.manager.numOfLines = 100;
    __weak typeof(self) weakSelf = self;
    self.manager.generrateViewBlock = ^(BulletView *view){
        
        [weakSelf addBarrageView:view];
    };
}

- (void)Stop:(UIButton *)sender {
    [self.manager stop];
}

- (void)start:(UIButton *)sender {
    
    [self.manager start];
}

- (void)addaBarrage:(UIButton *)sender {
    [self.manager.dataSource insertObject:[NSString stringWithFormat:@"弹幕 %d", arc4random()%1000] atIndex:0];
    [self.manager showNewBullet];
}

//新增一条动画
- (void)addBarrageView:(BulletView *)view {
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    view.frame = CGRectMake(width, 150 + view.line * 50, CGRectGetWidth(view.bounds), CGRectGetHeight(view.bounds));
    [self.view addSubview:view];
    
    [view startAnimation];
}


@end
